package com.app.oral.util

import com.app.oral.model.User
import com.app.oral.service.ClassService

object UserResponseHelper {
    
    /**
     * 构建用户响应数据（标准格式）
     */
    fun buildUserResponse(user: User, className: String? = null): Map<String, Any> {
        return mapOf<String, Any>(
            "id" to (user.id ?: 0),
            "openid" to (user.openid ?: ""),
            "nickname" to user.nickname,
            "avatarUrl" to user.avatarUrl,
            "realName" to user.realName,
            "gender" to user.gender.toString(),
            "userType" to (user.userType?.toString() ?: ""),
            "classId" to (user.classId ?: 0),
            "className" to (className ?: ""),
            "accountStatus" to user.accountStatus.toString(),
            "isActivated" to user.isActivated,
            "createdTime" to (user.createdTime?.toString() ?: ""),
            "updatedTime" to (user.updatedTime?.toString() ?: ""),
            "activatedTime" to (user.activatedTime?.toString() ?: ""),
            "lastLoginTime" to (user.lastLoginTime?.toString() ?: "")
        )
    }
    
    /**
     * 批量构建用户响应数据列表，自动获取班级名称
     */
    fun buildUserResponseList(users: List<User>, classService: ClassService): List<Map<String, Any>> {
        return users.map { user ->
            // 获取所有管理班级的名称
            val classNames = user.getClassIdList().mapNotNull { classId ->
                classService.getClassById(classId)?.className
            }.joinToString(", ")
            
            buildUserResponse(user, classNames.ifEmpty { null })
        }
    }
}